package controller

import (
	"github.com/gofiber/fiber"
	jsoniter "github.com/json-iterator/go"
	"gowork/cashier/model"
	"strconv"
)

type CashierController struct {
	Store *model.Store
	Consumption *model.Consumption
	IntegralRule *model.IntegralRule
	MemberInformation *model.MemberInformation
}

func (cc *CashierController)QueryPaymentMethod(c *fiber.Ctx){

	cc.Store = new(model.Store)

	storeIdStr := c.FormValue("storeId")
	storeId, err := strconv.ParseInt(storeIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := cc.Store.QueryPaymentMethod(storeId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": 200,
		"data": res,
		"message": "成功",
	})
	return
}

func (cc *CashierController)QueryCashierRule(c *fiber.Ctx){

	cc.Store = new(model.Store)

	storeIdStr := c.FormValue("storeId")
	storeId, err := strconv.ParseInt(storeIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := cc.Store.QueryCashierRule(storeId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": 200,
		"data": res,
		"message": "成功",
	})
	return
}

func (cc *CashierController)QueryIntegralRule(c *fiber.Ctx){

	cc.IntegralRule = new(model.IntegralRule)

	storeIdStr := c.FormValue("storeId")
	storeId, err := strconv.ParseInt(storeIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := cc.IntegralRule.QueryIntegralRule(storeId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": 200,
		"data": res,
		"message": "成功",
	})
	return
}

func (cc *CashierController)InsertIntegralRule(c *fiber.Ctx){

	cc.Store = new(model.Store)

	consumptionModeListStr := c.FormValue("consumptionModeList")
	var consumptionModeList []*model.ConsumptionMode
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(consumptionModeListStr), &consumptionModeList)

	storeIdStr := c.FormValue("storeId")
	storeId, err := strconv.ParseInt(storeIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := cc.Store.InsertIntegralRule(consumptionModeList, storeId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (cc *CashierController)QueryCashSetting(c *fiber.Ctx){

	cc.Consumption = new(model.Consumption)

	customerTypeIdStr := c.FormValue("customerTypeId")
	customerTypeId, err := strconv.ParseInt(customerTypeIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	storeIdStr := c.FormValue("storeId")
	storeId, err := strconv.ParseInt(storeIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := cc.Consumption.QueryCashSetting(customerTypeId, storeId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": 200,
		"data": res,
		"message": "成功",
	})
	return
}

func (cc *CashierController)PlaceAnOrder(c *fiber.Ctx){

	cc.Consumption = new(model.Consumption)

	consumptionStr := c.FormValue("consumption")
	var consumption model.Consumption
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(consumptionStr), &consumption)

	res , err := cc.Consumption.PlaceAnOrder(&consumption)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (cc *CashierController)PlaceAnOrderAsync(c *fiber.Ctx){

	cc.Consumption = new(model.Consumption)

	consumptionStr := c.FormValue("consumption")
	var consumption model.Consumption
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(consumptionStr), &consumption)

	res , err := cc.Consumption.PlaceAnOrderAsync(&consumption)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (cc *CashierController)Revoke(c *fiber.Ctx){

	cc.Consumption = new(model.Consumption)

	consumptionIdStr := c.FormValue("consumptionId")
	consumptionId, err := strconv.ParseInt(consumptionIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := cc.Consumption.Revoke(consumptionId)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (cc *CashierController)CancellationAudit(c *fiber.Ctx){

	cc.Consumption = new(model.Consumption)

	cancellationRecodesIdStr := c.FormValue("cancellationRecodesId")
	cancellationRecodesId, err := strconv.ParseInt(cancellationRecodesIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	pnStr := c.FormValue("pn")
	pn, err := strconv.Atoi(pnStr)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := cc.Consumption.CancellationAudit(cancellationRecodesId, pn)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (cc *CashierController)CancellationAuditAsync(c *fiber.Ctx){

	cc.Consumption = new(model.Consumption)

	cancellationRecodesIdStr := c.FormValue("cancellationRecodesId")
	cancellationRecodesId, err := strconv.ParseInt(cancellationRecodesIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	pnStr := c.FormValue("pn")
	pn, err := strconv.Atoi(pnStr)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := cc.Consumption.CancellationAuditAsync(cancellationRecodesId, pn)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (cc *CashierController)CourseConsumption(c *fiber.Ctx){

	cc.Consumption = new(model.Consumption)

	consumptionStr := c.FormValue("consumption")
	var consumption model.Consumption
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(consumptionStr), &consumption)

	res , err := cc.Consumption.CourseConsumption(&consumption)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (cc *CashierController)CourseConsumptionAsync(c *fiber.Ctx){

	cc.Consumption = new(model.Consumption)

	consumptionStr := c.FormValue("consumption")
	var consumption model.Consumption
	jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(consumptionStr), &consumption)

	res , err := cc.Consumption.CourseConsumptionAsync(&consumption)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (cc *CashierController)CourseRefund(c *fiber.Ctx){

	cc.MemberInformation = new(model.MemberInformation)

	MemberInformationIdStr := c.FormValue("memberInformationId")
	MemberInformationId, err := strconv.ParseInt(MemberInformationIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	MemberPurchasePackageList := c.FormValue("memberPurchasePackageList")

	rcStr := c.FormValue("rc")
	rc, err := strconv.Atoi(rcStr)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := cc.MemberInformation.CourseRefund(MemberInformationId, MemberPurchasePackageList, rc)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": nil,
		"message": res["message"],
	})
	return
}

func (cc *CashierController)ReviewRefund(c *fiber.Ctx){

	cc.MemberInformation = new(model.MemberInformation)

	refundRecordsIdStr := c.FormValue("refundRecordsId")
	refundRecordsId, err := strconv.ParseInt(refundRecordsIdStr, 10, 64)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	adoptStr := c.FormValue("adopt")
	adopt, err := strconv.Atoi(adoptStr)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	res , err := cc.MemberInformation.ReviewRefund(refundRecordsId, adopt)
	if err != nil{
		c.JSON(fiber.Map{
			"code": 500,
			"data": nil,
			"message": err.Error(),
		})
		return
	}

	c.JSON(fiber.Map{
		"code": res["code"],
		"data": res["data"],
		"message": res["message"],
	})
	return
}
















